; NAME:
; Read_MZT_File_Multi
;
; PURPOSE:
; Reads in MZT formated files and appends the data together
;
; CATEGORY:
; Read
;
; CALLING SEQUENCE:
; Read_MZT_File_Multi_AS, FILES = Files, Output
;
; INPUTS:
; Files = The files to be read
;
; OUTPUTS:
; The data contained in the files
;
; SIDE EFFECTS:
;
; RESTRICTIONS:
; Assumes that all the arrays have matching dimensions
; does no checking for this!
;
; PROCEDURE:
;
; MODIFICATION HISTORY:
;   Written Feb 19, 2014 by as13988

PRO Read_MZT_File_Multi, FILES = Files, Output, LON_NCNAME = lon_ncname,$
  LAT_NCNAME = lat_ncname, FLUX_NCNAME = flux_ncname,  DATE_NCNAME = date_ncname, DATESEC_NCNAME = datesec_ncname, GLOBAL_ATTS = Global_Atts


  if n_elements(Files) eq 0 then Files = DIALOG_PICKFILE(/MULTIPLE_FILES, FILTER='*_MZT.nc',TITLE='Select MZT files to read in')
  
  ; Extract information about the file
  ; This uses the first file and assumes that all the files are of the same format
  gauge_flux_extract_file_info, Files[0], DimNames, DimSizes,  VarInfo, LON_NCNAME = lon_ncname,$
    LAT_NCNAME = lat_ncname, FLUX_NCNAME = flux_ncname,  DATE_NCNAME = date_ncname, DATESEC_NCNAME = datesec_ncname, SKIP_PS = 1, GLOBAL_ATTS = Global_Atts
    
  lat=mr_ncget(lat_ncname, filename=Files[0])
  lon=mr_ncget(lon_ncname, filename=Files[0])
  
  Flux = !null
  DateAll = !null
  Time = !null
  FileNames = !null
  
  for i = 0L, N_elements(Files)-1 do begin
  
    Filenames = [FileNames, Files[i]]
    
    ; Extract the data from the flux file
    Flux_i=mr_ncget(flux_ncname, filename=Files[i]) ;kg/m2/s
    date_i=mr_ncget(date_ncname, filename=Files[i])
    Time_i=mr_ncget(datesec_ncname, filename=Files[i])
    
    ; collate
    Flux = [[[flux]],[[flux_i]]]
    DateAll = [DateAll, Date_i]
    Time = [Time, Time_i]
    
  endfor
  
  Output = { Date : DateAll, $
    Time : Time, $
    Lat : Lat, $
    Lon : Lon, $
    Flux : Flux }
    
END